package com.lwj.config;

import com.lwj.filter.LogFilter;
import com.lwj.filter.LoginFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;


@Configuration
@EnableWebMvc
@EnableSwagger2
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private HandlerInterceptor projectInterceptor;

    //解决  No mapping for GET /favicon.ico 访问静态资源图标
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/META-INF/resources/");
    }
    @Bean
    public FilterRegistrationBean sessionFilter(){
        FilterRegistrationBean registration = new FilterRegistrationBean(new LoginFilter());
        registration.addInitParameter("excludedPath","/api/user/login,/swagger-ui.html,/api/hello/*,/api/mysql/*");
        registration.addUrlPatterns("/api/*");
        return registration;
    }
    @Bean(name = "multipartResolver")
    public MultipartResolver multipartResolver(){
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setDefaultEncoding("UTF-8");
        resolver.setResolveLazily(true);
        resolver.setMaxInMemorySize(40960);
        //上传文件大小 50M 50*1024*1024
        resolver.setMaxUploadSize(50*1024*1024);
        return resolver;
    }

    @Bean
    public FilterRegistrationBean<LogFilter> registerAuthFilter() {
        LogFilter logFilter = new LogFilter();
        logFilter.setMaxLength(10000);
        String[] uriPatterns = { "*.do", "*.action" };
        logFilter.setUriPatterns(uriPatterns);
        String[] excludeUriPatterns = { "*.html", "*.js" };
        logFilter.setExcludeUriPatterns(excludeUriPatterns);
        String[] downContentType = { "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf8", "application/force-download;charset=utf-8" };
        logFilter.setDownContentType(new ArrayList<>(Arrays.asList(downContentType)));
        //
        FilterRegistrationBean<LogFilter> registration = new FilterRegistrationBean<LogFilter>();
        registration.setFilter(logFilter);
        registration.addUrlPatterns("/*");
        registration.setName("logFilter");
        // 值越小，Filter越靠前。
        registration.setOrder(1);
        return registration;
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //配置拦截器
        registry.addInterceptor(projectInterceptor).addPathPatterns("/api/hello" );
    }

}

